package ru.spbu.mm.diploma.client.controller;

import ru.spbu.mm.diploma.common.entity.User;
import ru.spbu.mm.diploma.common.entity.Userrole;
import ru.spbu.mm.diploma.common.facade.UserFacadeImpl;
import ru.spbu.mm.diploma.common.resources.MessageFactory;
import ru.spbu.mm.diploma.server.dao.DAOBean;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIForm;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class AdminController {
    private UIForm newUserForm;
    private User user = new User();
    private List<SelectItem> userroleList;

    public String save() {
        if (this.validateUser()) {
            UserFacadeImpl.getInstance().addUser(user);
            //TODO add popup window with information about adding
        }
        user = new User();
        return null;
    }

    public String cancel() {
        user.setUsername("");
        user.setPassword("");
        return null;
    }

    private boolean validateUser() {
        boolean isValid = true;
        FacesContext context = FacesContext.getCurrentInstance();
        MessageFactory mf = new MessageFactory();
        if (user.getUsername().length() < 5) {
            context.addMessage("newUserForm:username",
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, mf.getMessage("errorUsername"), null));

            isValid = false;
            user.setUsername("");

        } else {
            if (UserFacadeImpl.getInstance().getUserWithUsername(this.getUser().getUsername()) != null) {
                isValid = false;
                context.addMessage("newUserForm:username",
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, mf.getMessage("sameUsername"), null));
                user.setUsername("");
            }
        }
        if (user.getPassword().length() < 5) {
            context.addMessage("newItemForm:password",
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, mf.getMessage("errorPassword"), null));
            isValid = false;
            user.setPassword("");
        }
        return isValid;
    }


    public UIForm getNewUserForm() {
        return newUserForm;
    }

    public void setNewUserForm(UIForm newUserForm) {
        this.newUserForm = newUserForm;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<SelectItem> getUserroleList() {
        List<Userrole> userRoles =  DAOBean.userroleDAO.getUserroles();
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (Userrole uRole : userRoles) {
            items.add(new SelectItem(uRole.getId().toString(), uRole.getDescription()));
        }
        return items;
    }

    public void setUserroleList(List<SelectItem> userroleList) {
        this.userroleList = userroleList;
    }
}
